package cn.blossoming.service.user.impl;

import cn.blossoming.dao.domain.UserEntity;
import cn.blossoming.dao.domain.UserFansEntity;
import cn.blossoming.service.base.SessionService;
import cn.blossoming.service.base.impl.BaseServiceImpl;
import cn.blossoming.service.base.utils.PageUtils;
import cn.blossoming.service.dto.User;
import cn.blossoming.service.user.UserFansService;
import cn.blossoming.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl extends BaseServiceImpl<UserEntity, Integer> implements UserService, SessionService {

    @Autowired
    private UserFansService fansService;

    @Override
    public User getUserByPhone(String phone) {
        Example<UserEntity> example = Example.of(UserEntity.builder().phone(phone).build());
        if (!this.exists(example)) {
            long count = this.count();
            String username = "匿名用户".concat(String.valueOf(count + 10000));
            UserEntity insertUser = UserEntity.builder().name(username).phone(phone).build();
            this.save(insertUser);
        }
        UserEntity user = this.findOne(example);
        return new User(user);

    }

    @Override
    public List<User> findFans(int page, int prePage) {
        UserEntity userEntity = this.findOne(getCurrentUserId());
        List<UserFansEntity> userFans = userEntity.getUserFans();
        return PageUtils.toPage(userFans, page, prePage).stream().map(UserFansEntity::getUser).map(User::new).collect(Collectors.toList());
    }

    @Override
    public List<User> findFollow(int page, int prePage) {
        UserEntity userEntity = this.findOne(getCurrentUserId());
        List<UserFansEntity> userFans = userEntity.getUserFollows();
        return PageUtils.toPage(userFans, page, prePage).stream().map(UserFansEntity::getUser).map(User::new).collect(Collectors.toList());
    }

    @Override
    public User getUserById(int id) {
        UserEntity userEntity = this.findOne(id);
        return new User(userEntity);
    }

    @Override
    @Transactional
    public void addFollowUser(int id) {
        UserEntity follow = this.findOne(id);
        follow.setFansNum(follow.getFansNum() + 1);
        this.save(follow);
        UserEntity user = this.findOne(getCurrentUserId());
        user.setFollowNum(user.getFollowNum() + 1);
        this.save(user);

        UserFansEntity userFansEntity = new UserFansEntity();
        userFansEntity.setUser(user);
        userFansEntity.setFollow(follow);
        fansService.save(userFansEntity);


    }

    @Override
    public void delFollowUser(int id) {
        UserEntity follow = this.findOne(id);
        follow.setFansNum(follow.getFansNum() -1);
        this.save(follow);
        UserEntity user = this.findOne(getCurrentUserId());
        user.setFollowNum(user.getFollowNum() - 1);
        this.save(user);

        UserFansEntity userFansEntity = new UserFansEntity();
        userFansEntity.setUser(user);
        userFansEntity.setFollow(follow);
        fansService.delete(userFansEntity);
    }

}
